package com.qianxun.module.datasource1.admin.rest;

import com.qianxun.module.datasource1.admin.service.SysDataModifyLockService;
import com.qianxun.module.datasource1.admin.util.SysMenuRoleUtil;
import com.qianxun.module.base.constant.Result;
import com.qianxun.framework.common.util.ObjectUtil;
import com.qianxun.module.datasource1.admin.service.SysMenuRoleService;
import com.qianxun.module.datasource1.admin.entity.SysMenuRole;
import com.qianxun.module.datasource1.admin.dto.SysMenuRoleDTO;
import com.qianxun.module.base.bo.ZTreeNode;
import com.qianxun.module.base.bo.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* com.zch.admin.base.entity.SysRole模型http接口处理
*
* @author: admin
* @date: 2019-11-13
* @version: 1.0.0
*/
@RestController
@RequestMapping(value = {"/admin/menuRole"} ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class SysMenuRoleRestController {

    @Resource
    private SysMenuRoleService baseService;
    @Resource
    private SysDataModifyLockService sysDataModifyLockService;

    @PostMapping(value = {"/add"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result add(SysMenuRoleDTO query) {
        SysMenuRole SysRole = new SysMenuRole();
        ObjectUtil.switchAllFieldValue(SysRole, query);
        SysRole.init();
        baseService.save(SysRole);
        return Result.success();
    }

    @PostMapping(value = {"/delete/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result deleteById(@PathVariable(value = "id") Long id) {
        sysDataModifyLockService.checkLocked(SysMenuRoleUtil.dataLockKey(id));

        baseService.deleteById(id);
        return Result.success();
    }

    @PostMapping(value = {"/update/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result updateById(@PathVariable(value = "id") Long id, SysMenuRoleDTO query) {
        sysDataModifyLockService.checkLocked(SysMenuRoleUtil.dataLockKey(id));

        SysMenuRole SysRole = new SysMenuRole();
        ObjectUtil.switchAllFieldValue(SysRole, query);
        baseService.updateById(SysRole);
        return Result.success();
    }

    @RequestMapping(value = {"/search/page"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchPage(@RequestParam(required = false) Integer current ,@RequestParam(required = false) Integer size){
        PageInfo pageInfo = PageInfo.build(current,size);

        IPage<SysMenuRole> page =  baseService.page(new Page<>(pageInfo.getCurrent() ,pageInfo.getSize()));
        return Result.success(page).setCode(0);
    }

    @RequestMapping(value = {"/search/id/{id}"}, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Result searchOneById(@PathVariable(value = "id") Long id) {
        SysMenuRole SysRole = baseService.getById(id);
        return Result.success(SysRole);
    }


    @RequestMapping(value = {"/search/tree/{userId}"} ,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String searchMenuTreeByRoleId(@PathVariable(value = "userId") Long userId){
        List<ZTreeNode> list = baseService.searchTreeByUserId(userId);
        return ObjectUtil.toJson(list);
    }
}
